package cn.supermaster.management.common.configure;

import cn.supermaster.management.common.utils.ServletUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.common.configure
 * @ClassName: ServerConfiguration
 * @Description: [服务相关配置]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/10 12:56
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/

@Component
public class ServerConfiguration {
    /**
     * @Description: [获取完整的请求路径]
     * <p>包括：域名，端口，上下文访问路径</P>
     * @Title: getUrl
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Return: java.lang.String 服务地址
     */
    public String getUrl() {
        HttpServletRequest request = ServletUtils.getRequest();
        return getDomain(request);
    }

    /**
     * @Description: [获取请求domain]
     * <p></P>
     * @Title: getDomain
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: request
     * @Return: java.lang.String
     */
    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String requestURI = request.getRequestURI();
        String contextPath = request.getServletContext().getContextPath();

        int urlSize = url.length();// url长度
        int uriSize = requestURI.length();// uri长度
        //
        String domain = url.delete(urlSize - uriSize, urlSize)
                .append(contextPath).toString()
                .toString();
        //
        return domain;
    }
}